<?php

namespace App\Models\AppModels;

use App\Models\ParentModel;
use DB;
use Log;

/**
 * 合伙人用户model
 * @author puppet
 */
class User extends ParentModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'pt_user';

    /**
     * DB::直接操作的数据库表名
     * @var string
     */
    protected $db_table = 'pt_user';

    /**
     * 主键 默认id。
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指定是否模型应该被戳记时间。
     *
     * @var bool
     */
    public $timestamps = false;
	
	//修改雷达的状态
    public function editLeiDa($date){
    //    print_r($date);exit();
        $uid=$date['uid'];
        $id=$date['ldid'];
        $tatus=isset($date['status']) ? $date['status']:0;
        $update_time= date('Y-m-d H:i:s', time());
        $re=DB::table('pt_user_info')->where('id',$id)->where('partner_id',$uid)->update(['status'=>$tatus,'info_date'=>$update_time]);
        return $re;
    }
    //修改关键词的状态
    public function editKeyword($date){
        //    print_r($date);exit();
        $uid=$date['uid'];
        $id=$date['keyid'];
        $tatus=isset($date['status']) ? $date['status']:0;
        $update_time= date('Y-m-d H:i:s', time());
        if($date['type']==1){
            $re=DB::table('pt_keyword')->where('id',$id)->where('user_id',$uid)->update(['status'=>$tatus,'update_time'=>$update_time]);
        }elseif($date['type']==2){
            $re=DB::table('pt_keyword_conent')->where('id',$id)->where('user_id',$uid)->update(['status'=>$tatus,'update_time'=>$update_time]);
        }else{
            return false;
        }
        return $re;
    }
	
    //获取对应的粉丝数量
    public function getFanscount($open_id)
    {

        $countsql = " select count(id) as count from pt_fans where p_open_id='$open_id' ";
        $count = DB::select($countsql);
        //    echo '<pre/>'; echo 111; print_r($count);
        if (!empty($count)) {
            $countx = 0;
            foreach ($count as $k => $v) {
                $countx += $v['count'];
            }
        } else {
            return 0;
        }
        return $countx;
    }
    //查询是否存在对应id的用户
    public function checkUser($id){
        $sql=" select id from pt_partner where id=$id and status=1";
        $re=DB::select($sql);
      	$res=isset($re[0]['id']) ? $re : false;
      	return $res;

    }

    //获取视频数量
    public function getVideocount($open_id)
    {
        $countsql = " select count(id) as count from pt_dy_user_qyh_video where open_id='$open_id' ";
        $count = DB::select($countsql);
        //   echo '<pre/>'; echo 222; print_r($count);
        if (!empty($count)) {
            $countVideo = 0;
            foreach ($count as $k => $v) {
                $countVideo += $v['count'];
            }
        } else {
            return 0;
        }
        return $countVideo;
    }
    //获取曝光量
    public function getBgcount($open_id)
    {
        $bgcountsql = " select play_count from pt_dy_user_qyh_video where open_id='$open_id' ";
        $bgcount = DB::select($bgcountsql);
        //   echo '<pre/>'; echo 333; print_r($bgcount);
        if (!empty($bgcount)) {
            $count = 0;
            foreach ($bgcount as $k => $v) {
                $count += $v['play_count'];
            }
        } else {
            return 0;
        }
        return $count;
    }
    //获取评论数
    public function getcommentcount($open_id)
    {
        $bgcountsql = " select comment_count from pt_dy_user_qyh_video where open_id='$open_id' ";
        $bgcount = DB::select($bgcountsql);
        //   echo '<pre/>'; echo 333; print_r($bgcount);
        if (!empty($bgcount)) {
            $count = 0;
            foreach ($bgcount as $k => $v) {
                $count += $v['comment_count'];
            }
        } else {
            return 0;
        }
        return $count;
    }
    //获取私信数
    public function getprivatecount($open_id)
    {
        $bgcountsql = " select count(*) as count from pt_dy_privateletter where popenid='$open_id' ";
        $bgcount = DB::select($bgcountsql);
        return $bgcount[0]['count'];
    }
    //根据对应的openid获取accesstoken
    public function getaccesstoken($open_id){
       $sql=" select access_token from pt_dy_access_token where open_id='$open_id' ";
       $token=DB::select($sql);
       return $token;
    }

    //根据openid获取昵称
    public function getnickname($openid){
        $sql=" select nickname from pt_user where open_id='$openid'";
        $nickname=DB::select($sql);
        return $nickname;
    }

    //思路 先获取对应的openid 然后再循环里面调用方法获取对应的数据
    /**
     * 翻页展示所有信息
     *    授权账号列表
     */
    public function getPageData($params)
    {
        $partner_id = session('vip_user')[0]['admin_id'];
        $sql = "SELECT a.id,a.type,a.nickname,a.avatar,a.country,a.city,a.open_id,a.gender,a.e_account_role,a.update_time,a.status,b.status as zt FROM pt_user as a ";
        $sql .= " left join pt_dy_access_token as b on a.open_id=b.open_id WHERE a.status=1 and a.partner_id=? ";
        $openidinfo = DB::select($sql, [$partner_id]);
        $numVideo = $this->numVideo();
        $html = '';
        foreach ($openidinfo as $k => $v) {
            $fanscount = $this->getFanscount($v['open_id']);//获取对应的数据
            $videocount = $this->getVideocount($v['open_id']);
            $bgcount = $this->getBgcount($v['open_id']);
            $accesstoken=$this->getaccesstoken($v['open_id']);
            $commentcount=$this->getcommentcount($v['open_id']);
            $private=$this->getprivatecount($v['open_id']);
            if ($v['type'] == 1) {
                $v['type'] = '抖音';
            } elseif ($v['type'] == 2) {
                $v['type'] = '快手';
            }
            $html .= "<div style=\"height: 140px;margin: 10px\" ><ul class=\"video_fabu\" >";

            if ($v['e_account_role']=='认证企业号'){
                $html .= "<span style=\"position: absolute;\"><img style=\"width: 35px;height: 35px;\" src=\"/images/lv.png\" alt=''></span>";
                $html.="<li class=\"video_list\" style=\"background-color:#fff\">";
            }else {
                $html.="<li class=\"video_list\" style=\"background-color:#f4f4f4\">";
            }
            $html .= "<a class=\"video_img\" href=\"javascript:;\" style=\"padding-top:24px\"><img src=\"{$v['avatar']}\" width=\"100px\" height=\"100px\"></a>";
            $html .= "<div class=\"video_main\" style=\"width: 55%;padding: 0;border-right:1px solid rgb(230, 230, 230);\">";
            $html .= "<h2><a href=\"javascript:;\" class=\"type\">{$v['type']}</a><a href=\"javascript:;\" class=\"name\">{$v['nickname']}</a></h2>";
            $html .= "<ul class=\"video_num\">";
            $html .= "<li class=\"count\"  style=\"width: 20%;padding: 0;text-align: center\"><span style=\"padding: 16px;margin: 0px\">粉丝数</span><p>{$fanscount}</p></li>";
            $html .= "<li class=\"count\"  style=\"width: 20%;padding: 0;text-align: center\"><span style=\"padding: 16px;margin: 0px\">视频数</span><p>{$videocount}</p></li>";
            $html .= "<li class=\"count\"  style=\"width: 20%;padding: 0;text-align: center\"><span style=\"padding: 16px;margin: 0px\">曝光量</span><p>{$bgcount}</p></li>";
            $html .= "<li class=\"count\"  style=\"width: 20%;padding: 0;text-align: center\"><span style=\"padding: 16px;margin: 0px\">评论数</span><p>{$commentcount}</p></li>";
            $html .= "<li class=\"count\"  style=\"width: 20%;padding: 0;text-align: center\"><span style=\"padding: 16px;margin: 0px\">私信数</span><p>{$private}</p></li></ul></div>";
            $html .= "<div  style=\"width: 47%\">";
            if ($numVideo) {
                $html .= "<button onclick=\"ceshi_qiniu('{$v['open_id']}','{$v['nickname']}')\" style=\"margin-top:20px;margin-left:30px;background:#red;cursor:default;width: 90%;text-align: center\" >";
                $html .= "<svg class=\"icon\" aria-hidden=\"true\" style=\"width: 0px\"><use xlink:href=\"#icon-shipin\"></use></svg>发布视频</button>";
            } else {
                $html .= "<button  style=\"margin-top:20px;margin-left:30px;background:#ddd;cursor:default;width: 90%;text-align: center\" disabled=\"\">";
                $html .= "<svg class=\"icon\" aria-hidden=\"true\" style=\"width: 0px\"><use xlink:href=\"#icon-shipin\"></use></svg>额度不足</button>";
            }
            $html .= "<span class=\"buttonhy\">";
            if ($v['e_account_role']=='认证企业号'){
                $html .= "<a style=\"margin-left: 32px;margin-right: 0px\"  href=\"video/{$v['open_id']}\" ><button  class=\"material_smallbox\"  >视频管理</button></a>";
            //   $html .= "<a  style=\"margin-left: 2px\" href=\"javascript:void(0);\"  ><button  class=\"material_smallbox\"  onclick=\"sxtanc('{$accesstoken[0]['access_token']}','{$v['open_id']}')\">一键私信</button></a>";
                $html .= "<a  style=\"margin-left: 2px\" href=\"fans/{$v['open_id']}\"  ><button  class=\"material_smallbox\"  >粉丝管理</button></a>";
                $html .= "<a  style=\"margin-left: 2px\" href=\"following/{$v['open_id']}\"  ><button  class=\"material_smallbox\"  >关注管理</button></a>";
             $html .= "<a  style=\"margin-left: 2px\" href=\"replayinfo/{$v['open_id']}\"  ><button  class=\"material_smallbox\"  >自动回复</button></a>";
                $html .= "<a  style=\"margin-left: 2px\" href=\"enterprise/{$v['open_id']}\"  ><button  class=\"material_smallbox\"  >意向客户</button></a>";
           //     $html .= "<a  style=\"margin-left: 2px\" href=\"enterprise/{$v['open_id']}\"  ><button  class=\"material_smallbox\"  >卡片消息</button></a>";
            }else{
                $html .= "<a style=\"margin-left: 32px;margin-right: 0px\"  href=\"video/{$v['open_id']}\" ><button  class=\"material_smallbox\"  >视频管理</button></a>";
                $html .= "<a  style=\"margin-left: 17px\" href=\"ptfans/{$v['open_id']}\"  ><button  class=\"material_smallbox\"  >粉丝管理</button></a>";
                $html .= "<a  style=\"margin-left: 17px\" href=\"ptfollowing/{$v['open_id']}\"  ><button  class=\"material_smallbox\"  >关注管理</button></a>";
            }
            $html .= "</span>";
            $html .= "</div>";
            $html.="<div class=\"Del\"><i class=\"layui-icon\" style=\"font-size: 34px;cursor:pointer;\" onclick='deleteuser({$v['id']},-1)' >ဇ</i></div></div>";
            $html .= "</li></ul>";

        }
        return $html;

    }

    //剩余视频上传数
    function numVideo()
    {
        $partner_id = session('vip_user')[0]['admin_id'];

        //套餐视频上传总数
        $numAll = $this->mealAll();
        $numAll = $numAll['num1'];

        //已使用视频上传数
        $num = $this->numVideoUp();

        return ($numAll - $num) > 0 ? ($numAll - $num) : 0;
    }

    //已使用视频上传数
    function numVideoUp()
    {
        $partner_id = isset(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : 0;
        $num = DB::select("select count(id) as count from pt_dy_user_video where partner_id=?", [$partner_id]);
        return isset($num[0]['count']) ? $num[0]['count'] : 0;
    }


    //剩余视频雷达数
    function monitorVideo()
    {
        $partner_id = session('vip_user')[0]['admin_id'];

        //套餐视频达总数
        $numAll = $this->mealAll();
        $numAll = $numAll['num3'];

        //已使用的视频雷达数
        $num = $this->monitorVideoUp();

        return ($numAll - $num) > 0 ? ($numAll - $num) : 0;
    }

    //已使用视频雷达数
    function monitorVideoUp()
    {
        $partner_id = isset(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : 0;
        $num = DB::select("select count(id) as count from pt_user_info where system_type=0 and partner_id=? and info_type=2", [$partner_id]);
        return isset($num[0]['count']) ? $num[0]['count'] : 0;
    }


    //套餐详情
    function mealAll()
    {
        $partner_id = isset(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : 0;
        $sql = "select * from pt_partner_meal where partner_id=?";
        $re = DB::select($sql, [$partner_id]);
        return isset($re[0]['id']) ? $re[0] : false;
    }


    //已使用的询盘数
    function num3Comment()
    {
        $partner_id = isset(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : 0;

        $one = DB::select("select count(id) as count from pt_partner_comment where partner_id=?", [$partner_id]);
        $one = isset($one[0]['count']) ? $one[0]['count'] : 0;

        $two = DB::select("select count(id) as count from pt_user_info_comment where partner_id=?", [$partner_id]);
        $two = isset($two[0]['count']) ? $two[0]['count'] : 0;
        return $one + $two;
    }


    //剩余账户雷达数
    function monitorInfo()
    {
        //套餐主页数
        $mealAll = $this->mealAll();
        $num4 = $mealAll['num4'];

        //已使用账户雷达数
        $usedInfo = $this->monitorInfoUp();

        return $num4 - $usedInfo > 0 ? $num4 - $usedInfo : 0;
    }
	
    //已使用账户雷达数
    function monitorInfoUp()
    {
        $partner_id = isset(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : 0;
        $re = DB::select("select count(id) as count from pt_user_info where system_type=0 and partner_id=? and info_type=1", [$partner_id]);
        return isset($re[0]['count']) ? $re[0]['count'] : 0;
    }
	
	
    //剩余直播雷达数
    function liveUseInfo($count)
    {
        //套餐主页数 暂时取4的数组 到时候如果加套餐一个是num5
        $mealAll = $this->mealAll();
        $num4 = $mealAll['num4'];

        //已使用账户雷达数
        $usedInfo = $this->liveInfoUp();

        return $count - $usedInfo > 0 ? $count - $usedInfo : 0;
    }
	
    //已使用的直播数 m
    function liveInfoUp(){
        $partner_id = isset(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : 0;
        $re = DB::select("select count(id) as count from pt_user_info where system_type=0 and partner_id=? and info_type=1", [$partner_id]);
        return isset($re[0]['count']) ? $re[0]['count'] : 0;
    }
}